package be.khl.kevinvranken.breekuit;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

/**
 * Invoer afhandelen
 * Vooral wegen luiheid extenden we KeyAdapter en niet KeyListener
 * Naar indee van kevin glass
 * @author Kevin
 */
    public class InvoerAfhandeling extends KeyAdapter {

        /** The number of key presses we've had while waiting for an "any key" press */
        private Spel spel;
        private int pressCount = 1;


        public InvoerAfhandeling(Spel spel){
            this.spel=spel;
        }

        /**
         * Notification from AWT that a key has been pressed. Note that
         * a key being pressed is equal to being pushed down but *NOT*
         * released. Thats where keyTyped() comes in.
         *
         * @param e The details of the key that was pressed
         */
    @Override
        public void keyPressed(KeyEvent e) {
            // if we're waiting for an "any key" typed then we don't
            // want to do anything with just a "press"
            if (spel.isVerderGaanKnopActieNodig()) {
                return;
            }


            if (e.getKeyCode() == KeyEvent.VK_LEFT) {
                spel.setLinksGedrukt(true);
            }
            if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
                spel.setRechtsGedrukt(true);
            }
            if (e.getKeyCode() == KeyEvent.VK_SPACE) {
                spel.setSpatieKnop(true);
            }
        }

        /**
         * Notification from AWT that a key has been released.
         *
         * @param e The details of the key that was released
         */
    @Override
        public void keyReleased(KeyEvent e) {
            // if we're waiting for an "any key" typed then we don't
            // want to do anything with just a "released"
            if (spel.isVerderGaanKnopActieNodig()) {
                return;
            }

            if (e.getKeyCode() == KeyEvent.VK_LEFT) {
                spel.setLinksGedrukt(false);
            }
            if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
                spel.setRechtsGedrukt(false);
            }
            if (e.getKeyCode() == KeyEvent.VK_SPACE) {
                spel.setSpatieKnop(false);
            }
        }

        /**
         * Notification from AWT that a key has been typed. Note that
         * typing a key means to both press and then release it.
         *
         * @param e The details of the key that was typed.
         */
        public void keyTyped(KeyEvent e) {
            // if we're waiting for a "any key" type then
            // check if we've recieved any recently. We may
            // have had a keyType() event from the user releasing
            // the shoot or move keys, hence the use of the "pressCount"
            // counter.
            if (spel.isVerderGaanKnopActieNodig()) {
                if (pressCount == 1) {
                    // since we've now recieved our key typed
                    // event we can mark it as such and start
                    // our new game
                    spel.setVerderGaanKnopActieNodig(false);
                    spel.startGame();
                    pressCount = 0;
                } else {
                    pressCount++;
                }
            }

            // if we hit escape, then quit the game
            if (e.getKeyChar() == 27) {
                System.exit(0);
            }
        }
    }

